Add RealisticExecutionModel: spread + slippage price adjustment#514
Open
jgianfelice wants to merge 9 commits intomementum:masterfrom
Open
Add RealisticExecutionModel: spread + slippage price adjustment#514jgianfelice wants to merge 9 commits intomementum:masterfrom
jgianfelice wants to merge 9 commits intomementum:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Backtrader assumes orders fill at the exact closing price, which overstates strategy performance by ignoring bid-ask spread and market slippage.
Solution
Adds 'backtrader/executionmodels/' as a new subpackage containing 'RealisticExecutionModel', which adjusts the actual fill price before the order is submitted.
Usage
from backtrader.executionmodels.realistic import RealisticExecutionModel
exec_model = RealisticExecutionModel(spread=0.0005, slippage=0.0005)
adj_price = exec_model.adjust_price(self.data.close[0], is_buy=True)
self.buy(size=100, price=adj_price, exectype=bt.Order.Limit)
Parameters
Example & Results
A full worked example with real benchmark results is available in the contributor's fork under:
github.com/jgianfelice/backtrader/tree/master/samples/execution-model-realistic
Running SPY with a simple SMA-20 crossover (2020-2024):
No costs: +8.46% return, Sharpe -0.50, Max DD 8.10%
With costs: +1.22% return, Sharpe -8.84, Max DD 1.23%
Cost drag: -7.24% return